<?php

namespace App\Http\Controllers\Config;

use App\Models\Config\Game;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class GameController extends Controller
{
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
    }

    public function index(Request $request)
    {
        if ($request->ajax()) {

            $game= Game::first();

            $rows= [];
            foreach ($game["attributes"] as $key => $val){
                if($key == "_id" || $key == "updated_at") {
                    continue;
                }

                $val["key"] = $key;
                $rows[] = $val;
            }
            
            $data = [
                'rows' => $rows,
            ];

            return response()->json($data);

        } else {
            return view('/config/game/index');
        }

    }

    public function edit(Request $request)
    {
        $oper = $request->get('oper');
        $game = Game::first();

        if ($oper == "add"){

            $this->validate($request, [
                "key" => "required",
                "min_player_count" => "required|int",
                "free_trial_model" => "boolean",
                "cellscore" => "required|int",
                "exchange_threshold" => "required|int",
                "exchange_extra_reward" => "required|int",
                "default_user_score" => "required|int",
                "exchange_rate" => "numeric|min:0|max:1",
            ]);

            $key = $request->get('key');
            $min_player_count = intval($request->get("min_player_count"));
            $free_trial_model = boolval($request->get("free_trial_model"));
            $cellscore = intval($request->get("cellscore"));
            $exchange_threshold = intval($request->get("exchange_threshold"));
            $exchange_extra_reward = intval($request->get("exchange_extra_reward"));
            $exchange_rate = doubleval($request->get("exchange_rate"));
            $default_user_score = intval($request->get("default_user_score"));

            if ($game->$key) {
                return response()->json(["success" => false , "message" => "key已存在"]);
            }else{

                $game->$key = [
                    "min_player_count" => $min_player_count,
                    "free_trial_model" => $free_trial_model,
                    "cellscore" => $cellscore,
                    "exchange_threshold" => $exchange_threshold,
                    "exchange_extra_reward" => $exchange_extra_reward,
                    "exchange_rate" => $exchange_rate,
                    "default_user_score" => $default_user_score,
                ];
                $game->save();
            }
        }elseif ($oper == "edit"){

            $this->validate($request, [
                "min_player_count" => "required|int",
                "free_trial_model" => "boolean",
                "cellscore" => "required|int",
                "exchange_threshold" => "required|int",
                "exchange_extra_reward" => "required|int",
                "default_user_score" => "required|int",
                "exchange_rate" => "numeric|min:0|max:1",
            ]);

            $id = $request->get("id");
            $min_player_count = intval($request->get("min_player_count"));
            $free_trial_model = boolval($request->get("free_trial_model"));
            $cellscore = intval($request->get("cellscore"));
            $exchange_threshold = intval($request->get("exchange_threshold"));
            $exchange_extra_reward = intval($request->get("exchange_extra_reward"));
            $exchange_rate = doubleval($request->get("exchange_rate"));
            $default_user_score = intval($request->get("default_user_score"));

            $game->$id = [
                "min_player_count" => $min_player_count,
                "free_trial_model" => $free_trial_model,
                "cellscore" => $cellscore,
                "exchange_threshold" => $exchange_threshold,
                "exchange_extra_reward" => $exchange_extra_reward,
                "exchange_rate" => $exchange_rate,
                "default_user_score" => $default_user_score,
            ];
            $game->save();

        }elseif($oper == "del"){

            $key = $request->get('id');

            $game->drop($key);
        }

        return response()->json(["success" => true]);

    }
}
